package com.graduation.rescuematerial.controller.system;

import com.graduation.rescuematerial.service.ICatalogService;
import com.graduation.rescuematerial.service.MaterialService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/28 22:49
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/sys/material")
@Api(tags = "系统管理员——物资管理")
public class SysMaterialController {

    // 1、物资列表 √
    // 2、物资目录列表 √
    // 3、添加物资（添加物资交给仓库，平台只做管理）
    // 4、添加物资目录 √
    // 5、模糊查询物资
    // 6、模糊查询物资目录
    // 7、修改物资信息
    // 8、修改目录信息 √

    @Autowired
    private MaterialService materialService;
    @Autowired
    private UserService userService;
    @Autowired
    private ICatalogService catalogService;

    @ApiOperation("获取物资列表")
    @PostMapping("/get/all")
    @ResponseBody
    public RespBean getAllMaterial(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return materialService.getAllMaterial();
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("修改物资信息")
    @PostMapping("/update")
    @ResponseBody
    public RespBean updateMaterialInfo(@RequestBody MaterialVo materialVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=materialVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return materialService.updateMaterialInfo(materialVo.getBaseMaterial());
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }


    @ApiOperation("添加物资")
    @ResponseBody
    @PostMapping("/add/material")
    @Transactional(rollbackFor = Exception.class)
    public RespBean addMaterial(@RequestBody MaterialVo materialVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=materialVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                return materialService.addOne(materialVo.getBaseMaterial(),userVo.getCompanyName());
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("模糊查询物资")
    @ResponseBody
    @PostMapping("/fuzzy")
    public RespBean fuzzyQuery(@RequestBody FuzzyVo fuzzyVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=fuzzyVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && userVo.getAction().equals("system_administrator")){
                return materialService.fuzzyQuery(fuzzyVo.getKey());
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("物资种类下拉框")
    @ResponseBody
    @PostMapping("/catalog/dropDownBox")
    public RespBean catalogDropDownBox(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                return catalogService.catalogDropDownBox();
            }
            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非系统管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

}
